package com.smh.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    private JwtUtil() {

    }

    private static final String signKey = "nb666";
    private static final long expire = 3600000L;

    public static String getJwt(Map<String, Object> claims) {
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, signKey)//第一个参数是加密格式,第二个参数是要混入的密钥
                .setClaims(claims)//令牌的内容
                .setExpiration(new Date(System.currentTimeMillis() + expire))//有效期
                .compact();
        return jwt;
    }

    public static Claims parseJWT(String jwt) {
        Claims claims = Jwts.parser()
                .setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
